package com.example.admin.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.example.admin.base.PageResponse;
import com.example.admin.bean.LoginUserDetails;
import com.example.admin.bean.NursingPlan;
import com.example.admin.bean.NursingProject;
import com.example.admin.bean.NursingProjectPlan;
import com.example.admin.mapper.NursingPlanmapper;
import com.example.admin.mapper.NursingProjectmapper;
import com.example.admin.service.NursingPlanservice;
import com.example.admin.service.NursingProjectservice;
import com.example.admin.utils.UserThreadLocal;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ImpNursingPlan implements NursingPlanservice {
    @Autowired
    NursingPlanmapper nursingPlanmapper;


    @Override
    public PageResponse<NursingPlan> searchingforlist(Integer pagenum, Integer pagesize, NursingPlan nursingProject) {
        PageHelper.startPage(pagenum,pagesize);
        List<NursingPlan> nursingplans = nursingPlanmapper.searchingforlist(nursingProject);
        Page pages = (Page) nursingplans;
        PageResponse<NursingPlan> nursingPlanPageResponse = new PageResponse<>(pages);
        nursingPlanPageResponse.setRecords(nursingplans);
        int size = nursingPlanPageResponse.getRecords().size();
        nursingPlanPageResponse.setTotal(Long.parseLong(size+""));
        return nursingPlanPageResponse;
    }

    @Transactional
    @Override
    public void save(NursingPlan nursingPlan) {
        String subject = UserThreadLocal.getSubject();
        LoginUserDetails loginUserDetails = JSONObject.parseObject(subject, LoginUserDetails.class);
        Long userId = loginUserDetails.getId();
        nursingPlan.setCreateBy(userId);
        nursingPlanmapper.save(nursingPlan);

        List<NursingProjectPlan> nursingProjectPlans = nursingPlan.getNursingProjectPlans();
        for (NursingProjectPlan nursingProjectPlan : nursingProjectPlans) {
            nursingProjectPlan.setCreateBy(userId);
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        }

        // 保存护理项目列表
        nursingPlanmapper.savePlanAndProjectList(nursingProjectPlans);
    }


    @Override
    public NursingPlan findbyid(Long id) {
        NursingPlan nursingplan = nursingPlanmapper.findbyid(id);
        return nursingplan;
    }

    @Override
    public Integer edit(NursingPlan nursingProject) {
        return null;
    }

    @Override
    public Integer fixstatus(Long id, Integer status) {
        return null;
    }

    @Override
    public Integer delete(Long id) {
        return null;
    }
}
